/**
 * Project Name:mauto-web
 * File Name:ApiInterceptor.java
 * Package Name:com.inftech.mauto.jy.interceptor
 * Date:2017年6月16日下午5:00:48
 * Copyright (c) 2017, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.jy.interceptor;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.jy.service.TechnicianService;
import com.inftech.mauto.sys.entity.ResultJson;
import com.mingsoft.base.constant.Const;
import com.mingsoft.basic.constant.e.SessionConstEnum;
import com.mingsoft.basic.interceptor.BaseInterceptor;
import com.mingsoft.util.StringUtil;

import net.mingsoft.base.util.JSONObject;

/**
 * ClassName:ApiInterceptor <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Date:     2017年6月16日 下午5:00:48 <br/>
 * @author   Administrator
 * @version  
 * @since    JDK 1.7
 */
public class ApiInterceptor extends BaseInterceptor{
	@Resource
	private TechnicianService technicianServiceImpl;
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String token = request.getParameter("token");
		if(token==null || "".equals(token)){
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_401;
			ResultJson result = new ResultJson();
			if (statusCode != null) {
				result.setCode(String.valueOf(statusCode.toInt()));
			}
			result.setResult(true);
			result.setResultMsg(statusCode.toString());
			result.setResultData(null);
			String outJson = JSONObject.toJSON(result).toString();
			 response.setContentType("application/json;charset=utf-8");
			 PrintWriter printWriter =  response.getWriter();
			 printWriter.print(outJson);
			 printWriter.flush();
			 printWriter.close();
			logger.warn("用户未登录");
			return false;
		}
		String phone = technicianServiceImpl.getPhoneByToken(token);
		if(phone==null){
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_401;
			ResultJson result = new ResultJson();
			if (statusCode != null) {
				result.setCode(String.valueOf(statusCode.toInt()));
			}
			result.setResult(true);
			result.setResultMsg(statusCode.toString());
			result.setResultData(null);
			String outJson = JSONObject.toJSON(result).toString();
			 response.setContentType("application/json;charset=utf-8");
			 PrintWriter printWriter =  response.getWriter();
			 printWriter.print(outJson);
			 printWriter.flush();
			 printWriter.close();
			logger.warn("用户未登录");
			return false;
		}else{
			return true;
		}
	}
}

